SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROC [dbo].[SP_TXMVTO_CNTB_Q51]
/*-------------------------------------------------------------------*/    
/*----- Empresa           : OFISIS S.A.                          ----*/    
/*----- Cliente           : OFISIS S.A.                          ----*/    
/*----- Sistema           : Contabilidad                         ----*/    
/*----- Modulo            : Empresa                              ----*/    
/*----- Programa          : OBTIENE NUMERO DE ASIENTO DE TABLAS DE TESO  ----*/    
/*----- Nombre            : SP_TXMVTO_CNTB_Q51                   ----*/    
/*----- Script            : TXMVTO_CNTB                          ----*/    
/*----- Desarrollado por  : percy garcia                         ----*/    
/*----- Fecha             : 26/10/2006                           ----*/    
/*----- Base de Datos     : MS SQL Server                        ----*/    
/*----- Version BD        : 2005                                 ----*/    
/*-------------------------------------------------------------------*/    
/*-------------------------------------------------------------------*/        
/*----- Drop proc SP_TXMVTO_CNTB_Q51                             ----*/    
/*----- Grant all on SP_TXMVTO_CNTB_Q51 to PUBLIC                ----*/    
/*----- 
EXEC SP_TXMVTO_CNTB_Q51  '01','OFISIS','10/26/2006',1,2006,10,0.5
                                     ----*/    
/*-------------------------------------------------------------------*/    
@ISCO_EMPR       TD_VC_002,
@ISTI_AUXI_EMPR  TD_VC_001,
@ISCO_AUXI_EMPR  TD_VC_020,
@ISTI_DOCU       TD_VC_003,
@ISNU_DOCU       TD_VC_020, 
@ISTI_AUXI_CLIE  TD_VC_001,
@ISTI_AUXI_PROV  TD_VC_001,
@OSNU_CANC  	 TD_IN_001 OUT
As
Declare     
@VSST_TIPO_LETR TD_VC_001,
@VSNU_ASTO_INTE TD_VC_010,
@VSCO_UNID_INTE TD_VC_003, 
@VNNU_CANC 	TD_IN_001

 -- EVALUA SI ES LETRA U OTRO DOCUMENTO
 Select @VSST_TIPO_LETR = IsNull(ST_DOCU_0006, '')
 From TTDOCU_CNTB
 Where TI_DOCU = @ISTI_DOCU

 Select @VNNU_CANC = 0

 -- caso clientes            
 If @ISTI_AUXI_EMPR = @ISTI_AUXI_CLIE             
 Begin             
   -- SI ES LETRA             
   If @VSST_TIPO_LETR = 'S'             
     Select Distinct @VNNU_CANC = count(t2.ST_CANC)
     From TMLETR_CLIE t1, TTTIES_DOCU t2, TDCANJ_CLIE t3, TCCANJ_CLIE t4             
     Where t1.CO_EMPR = @ISCO_EMPR   
     And t1.CO_CLIE = @ISCO_AUXI_EMPR         
     And t1.NU_LETR_CLIE = @ISNU_DOCU            
     And t2.CO_ESTA_DOCU = t1.CO_ESTA_DOCU            
     And t2.ST_CANC = 'S'             
     And t3.CO_EMPR = t1.CO_EMPR
     And t3.NU_LETR_CLIE = t1.NU_LETR_CLIE
     And t4.CO_EMPR = t3.CO_EMPR
     And t4.CO_UNID_CONC = t3.CO_UNID_CONC
     And t4.CO_CLIE = t3.CO_CLIE 
     And t4.NU_CANJ = t3.NU_CANJ
     And t4.FE_CANJ = t3.FE_CANJ
     And t4.TI_SITU != 'ANU'

    -- SI NO ES LETRA 
    If @VSST_TIPO_LETR != 'S' 
      Select Distinct @VNNU_CANC = count(t2.ST_CANC)
      From TMDOCU_CLIE t1, TTTIES_DOCU t2 
      Where t1.CO_EMPR = @ISCO_EMPR 
      And t1.CO_CLIE = @ISCO_AUXI_EMPR 
      And t1.CO_TIPO_DOCU = @ISTI_DOCU 
      And t1.NU_DOCU_CLIE = @ISNU_DOCU 
      And t2.CO_ESTA_DOCU = t1.CO_ESTA_DOCU 
      And t2.ST_CANC = 'S' 
 End

 -- CASO PROVEEDORES 
 If @ISTI_AUXI_EMPR = @ISTI_AUXI_PROV            
  Begin    
   -- SI ES LETRA             
   If @VSST_TIPO_LETR = 'S'             
     Select Distinct @VNNU_CANC = count(t2.ST_CANC)
     From TMLETR_PROV t1, TTTIES_DOCU t2,TDCANJ_PROV t3,TCCANJ_PROV t4
     Where t1.CO_EMPR = @ISCO_EMPR            
     And t1.CO_PROV = @ISCO_AUXI_EMPR            
     And t1.NU_LETR_PROV = @ISNU_DOCU            
     And t2.CO_ESTA_DOCU = t1.CO_ESTA_DOCU            
     And t2.ST_CANC = 'S'            
     And t3.CO_EMPR = t1.CO_EMPR
     And t3.NU_LETR_PROV = t1.NU_LETR_PROV
     And t4.CO_EMPR = t3.CO_EMPR
     And t4.CO_UNID_CONC = t3.CO_UNID_CONC
     And t4.CO_PROV = t3.CO_PROV
     And t4.NU_CANJ = t3.NU_CANJ
     And t4.FE_CANJ = t3.FE_CANJ
     And t4.TI_SITU != 'ANU'

   -- SI NO ES LETRA
   If @VSST_TIPO_LETR != 'S'            
     Select Distinct @VNNU_CANC = count(t2.ST_CANC)
     From TMDOCU_PROV t1, TTTIES_DOCU t2             
     Where t1.CO_EMPR = @ISCO_EMPR 
     And t1.CO_PROV = @ISCO_AUXI_EMPR            
     And t1.CO_TIPO_DOCU = @ISTI_DOCU            
     And t1.NU_DOCU_PROV = @ISNU_DOCU            
     And t2.CO_ESTA_DOCU = t1.CO_ESTA_DOCU        
     And t2.ST_CANC = 'S'            
  End

Select @OSNU_CANC = @VNNU_CANC

Return 
/*-------- FIN DE PROCEDURE ---------*/


GO
